var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    num: 4,//后端给的分数,显示相应的星星  
    one_1: '',
    two_1: '',
    location: '',
    county: '',
    categoryList: util.categoryList,
    sliderList: [
      { selected: true, imageSource: 'http://up.enterdesk.com/edpic/7d/35/13/7d3513ecabdf1f7eb4f1407f0e82f23c.jpg' },
      { selected: false, imageSource: '../../images/2.jpg' },
      { selected: false, imageSource: 'http://pic1.win4000.com/wallpaper/9/538544be6ae36.jpg' },
    ]
  },
  onLoad: function (options) {

    this.setData({
      one_1: this.data.num,
      two_1: 5 - this.data.num
    })
    //定位当前城市
    this.getLocation();
    //获取用户信息
    wx.getUserInfo({
      success: function (res) {
        var log = Date.now();
        res.userInfo.logtime = util.formatTime(new Date(log));
        var userInfos = wx.getStorageSync('userInfos') || [];
        userInfos.unshift(res.userInfo);
        wx.setStorageSync('userInfos', userInfos);
      }
    })
  },
  //定位当前城市
  getLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //当前的经度和纬度
        let latitude = res.latitude
        let longitude = res.longitude
    
        wx.request({
          url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${app.globalData.tencentMapKey}`,
          success: res => {
            app.globalData.defaultCity = app.globalData.defaultCity ? app.globalData.defaultCity:res.data.result.ad_info.city;
            app.globalData.defaultCounty = app.globalData.defaultCounty ? app.globalData.defaultCounty :res.data.result.ad_info.district;
            app.globalData.defautlLat = res.data.result.ad_info.location.lat;
            app.globalData.defaultLong = res.data.result.ad_info.location.lng;
            app.globalData.defaultAddress = res.data.result.address;
            that.setData({
              location: app.globalData.defaultCity,
              county: app.globalData.defaultCounty
            });
          }
        })
      }
    })
  },
  //点击更改定位切换到城市页面
  jump: function () {
    console.log("xxccc");
  },
  goToDetail:function(e){
    console.log(e.currentTarget.dataset.name);
    wx.navigateTo({
      url: '../shopdetail/shopdetail?id=' + e.currentTarget.id + '&name=' + e.currentTarget.dataset.name,
    })
  },
  //点击跳转目录页面
  categoryClick:function(e){
    wx.navigateTo({
      url: '../category/category?title=' + e.currentTarget.dataset.flag + '&name=' + e.currentTarget.dataset.name,
    })
  },
  //轮播图绑定change事件，修改图标的属性是否被选中
  switchTab: function (e) {
    var sliderList = this.data.sliderList;
    var i, item;
    for (i = 0; item = sliderList[i]; ++i) {
      item.selected = e.detail.current == i;
    }
    this.setData({
      sliderList: sliderList
    });
  },
  // 用户点击右上角分享
  onShareAppMessage: function () {
    return {
      title: 'e 生活',
      desc: '分享个小程序，希望你喜欢☺️~',
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: "分享成功",
          duration: 1000,
          icon: "success"
        })
      }
    }
  }
})